<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    showFooter
    :title="getTitle"
    width="90%"
    @ok="handleSubmit"
    :showOkBtn="false"
    @close="handleClose"
  >
    <DisplayRules :mainId="dataRef?.id" :formCode="formCode" v-if="formCode" />
  </BasicDrawer>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref } from 'vue';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { useMessage } from '/@/hooks/web/useMessage';
  import DisplayRules from './displayRules/DisplayRules.vue';
  export default defineComponent({
    name: 'DisplayRulesDrawer',
    components: {
      BasicDrawer,
      DisplayRules,
    },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const isUpdate = ref(true);
      let dataRef = ref<Recordable>({});
      const formCode = ref('');
      const { createMessage } = useMessage();

      const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
        setDrawerProps({ confirmLoading: false });
        isUpdate.value = data?.isUpdate;
        dataRef.value = data?.record;
        formCode.value = data?.formCode;
        console.log('执行了', dataRef.value, formCode.value);
      });

      const getTitle = computed(() => `显示规则-${unref(dataRef)?.name}`);

      async function handleSubmit() {
        try {
          console.log('dataRef111', unref(dataRef));
          setDrawerProps({ confirmLoading: true });
          createMessage.success('操作成功');
          // closeDrawer();
          emit('success');
        } finally {
          setDrawerProps({ confirmLoading: false });
        }
      }

      const handleClose = () => {
        formCode.value = '';
      };

      return {
        registerDrawer,
        getTitle,
        handleSubmit,
        dataRef,
        formCode,
        handleClose,
      };
    },
  });
</script>
